package com.wuming.house.api;

import com.wuming.house.core.BusinessException;
import com.wuming.house.core.HouseType;
import com.wuming.house.core.JsonResult;
import com.wuming.house.model.*;
import com.wuming.house.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 楼盘明细信息的接口
 */
@Controller
@RequestMapping("/api/housedetail")
public class HouseDetailApi {

    private static Logger logger = LoggerFactory.getLogger(HouseDetailApi.class);

    @Autowired
    private HouseService houseService;

    @Autowired
    private HouseTagService houseTagService;

    @Autowired
    private HouseInfoService houseInfoService;

    @Autowired
    private HouseLayoutService houseLayoutService;

    @Autowired
    private UserCollectionService userCollectionService;

    @Autowired
    private HouseCommentService houseCommentService;

    /**
     * 收藏新房信息
     *
     * @param houseId 楼盘id
     * @return 楼盘
     */
    @ResponseBody
    @RequestMapping(value = "/collect/{houseId}", method = RequestMethod.GET)
    public JsonResult collect(@PathVariable Long houseId, @RequestParam Long userId) {
        try {
            List<UserCollection> collections = userCollectionService.getUserCollectionById(userId, houseId, HouseType.NEW_HOUSE.getValue());
            if (null != collections && !collections.isEmpty()) {
                return new JsonResult(collections.get(0), "收藏成功.", true);
            } else {
                UserCollection collection = new UserCollection();
                collection.setUserId(userId);
                collection.setHouseId(houseId);
                collection.setHouseType(HouseType.NEW_HOUSE.getValue());
                collection.setCreateDatetime(new Date());
                userCollectionService.saveUserCollection(collection);
                return new JsonResult(collection, "收藏成功.", true);
            }
        } catch (BusinessException be) {
            logger.error("收藏失败,异常:{}", be.getMessage());
            return new JsonResult(null, "收藏失败.", false);
        } catch (Exception e) {
            logger.error("收藏失败,异常:{}", e.getMessage());
            return new JsonResult(null, "收藏失败.", false);
        }
    }

    /**
     * 收藏新房信息
     *
     * @param houseId 楼盘id
     * @return 楼盘
     */
    @ResponseBody
    @RequestMapping(value = "/cancelcollect/{houseId}", method = RequestMethod.GET)
    public JsonResult cancelCollect(@PathVariable Long houseId, @RequestParam Long userId) {
        try {
            List<UserCollection> collections = userCollectionService.getUserCollectionById(userId, houseId, HouseType.NEW_HOUSE.getValue());
            if (null != collections && !collections.isEmpty()) {
                userCollectionService.removeUserCollectionById(collections.get(0).getId());
            }
            return new JsonResult(null, "取消收藏成功.", true);
        } catch (BusinessException be) {
            logger.error("取消收藏失败,异常:{}", be.getMessage());
            return new JsonResult(null, "取消收藏失败.", false);
        } catch (Exception e) {
            logger.error("取消收藏失败,异常:{}", e.getMessage());
            return new JsonResult(null, "取消收藏失败.", false);
        }
    }


    /**
     * 根据楼盘id查询楼盘信息
     *
     * @param houseId 楼盘id
     * @return 楼盘
     */
    @ResponseBody
    @RequestMapping(value = "/house/get/{houseId}", method = RequestMethod.GET)
    public JsonResult getHouseById(@PathVariable Long houseId) {
        try {
            House house = houseService.getHouseById(houseId);
            List<HouseTag> houseTags = houseTagService.getHouseTagByHouseId(houseId, HouseType.NEW_HOUSE.getValue());
            house.setHouseTags(houseTags);
            List<HouseInfo> houseInfos = houseInfoService.getHouseInfoByHouseId(houseId);
            house.setHouseInfos(houseInfos);
            List<HouseLayout> houseLayouts = houseLayoutService.getHouseLayoutByHouseId(houseId);
            house.setHouseLayouts(houseLayouts);
            return new JsonResult(house, "查询楼盘信息成功.", true);
        } catch (BusinessException be) {
            logger.error("查询楼盘信息失败,异常:{}", be.getMessage());
            return new JsonResult(null, "查询楼盘信息失败.", false);
        } catch (Exception e) {
            logger.error("查询楼盘信息失败,异常:{}", e.getMessage());
            return new JsonResult(null, "查询楼盘信息失败.", false);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/comment/save", method = RequestMethod.GET)
    public JsonResult saveComment(@RequestParam String houseId, @RequestParam Integer score, @RequestParam String message,
                                  @RequestParam Long userid) {
        try {
            HouseComment comment = new HouseComment();
            comment.setHouseId(Long.valueOf(houseId));
            comment.setHouseScore(score);
            comment.setHouseComment(message);
            comment.setCreateDatetime(new Date());
            comment.setUserid(userid);
            houseCommentService.saveHouseComment(comment);
            return new JsonResult(null, "保存评论成功.", true);
        } catch (BusinessException be) {
            logger.error("保存评论失败,异常:{}", be.getMessage());
            return new JsonResult(null, "保存评论失败.", false);
        } catch (Exception e) {
            logger.error("保存评论失败,异常:{}", e.getMessage());
            return new JsonResult(null, "保存评论失败.", false);
        }
    }
}
